import numpy as np
import sys
sys.path.append('../')

from optimizer.ctsp.ctsp_data import Data  # tsp data
from optimizer.ctsp.ga import TCGA  # two-part chromosome GA
from optimizer.ctsp.vns import VNS  # variable neighbor search
from optimizer.ctsp.aco import ACO  # ACO
from optimizer.ctsp.abc_optimizer import ABC  # ABC


import warnings

warnings.filterwarnings('ignore')
np.set_printoptions(linewidth=999, threshold=int(1e6), suppress=True)

if __name__ == '__main__':

    cases = ['',
             'data/TSPLIB_COLORED/N51_S15_C3.txt',  # 1
             'data/TSPLIB_COLORED/N51_S15_C4.txt',  # 2
             'data/TSPLIB_COLORED/N150_S45_C3.txt',  # 3
             'data/TSPLIB_COLORED/N150_S45_C4.txt',  # 4
             'data/TSPLIB_COLORED/N202_S60_C3.txt',  # 5
             'data/TSPLIB_COLORED/N202_S60_C4.txt',  # 6
             'data/TSPLIB_COLORED/N666_S199_C3.txt',  # 7
             'data/TSPLIB_COLORED/N666_S199_C4.txt',  # 8
             'data/TSPLIB_COLORED/N1002_S300_C3.txt',  # 9
             'data/TSPLIB_COLORED/N1002_S300_C4.txt',  # 10
             'data/TSPLIB_COLORED/N48_S14_C2.txt',  # 11
             'data/TSPLIB_COLORED/N48_S14_C5.txt',  # 12
             ]
    index = int(input('select case: '))
    print(cases[index])
    data = Data(cases[index])
    data.calculate_distances()

    method = 'abc'
    history = None

    for i in range(30):
        print(f'round {i}: ')
        if method == 'ga':
            ga = TCGA(data=data, NP=40, Pc=0.8, Pm=0.1, greedy_init=False)
            history = ga.optimize(iterations=10000)

        if method == 'vns':
            vns = VNS(data=data, neighbor_size=10, drop_prob=0.1)
            history = vns.optimize(iterations=10000)

        if method == 'aco':
            aco = ACO(data=data, NP=10, alpha=1, beta=2.5, rho=0.1, xee=0.1, q=0.9)
            history = aco.optimize(iterations=10000)

        if method == 'abc':
            abc = ABC(data=data, NP=40, drop_prob=0.2, p_bts=0.8, scout=100)
            history = abc.optimize(iterations=10000)

        np.savetxt(f'./zillion/fit_{method}_{i}.txt', history.gBest)
        np.savetxt(f'./zillion/route_{method}_{i}.txt', history.routes, fmt='%d')
